<?php

define ('MOD_PATH', dirname(__FILE__));

class Default_Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
    
    protected function _initAutoload() {
        
        // Add autoloader empty namespace
        $autoLoader = Zend_Loader_Autoloader::getInstance();
        
        $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
            'basePath' => APPLICATION_PATH, 'namespace' => '', 
            'resourceTypes' => array(            
                //'form' => array('path' => 'modules/admin/forms/', 'namespace' => 'Admin_'),
                array('path' => 'modules/default/forms/', 'namespace' => 'Form_'),
                'model' => array('path' => 'modules/default/models/', 'namespace' => 'Model_'),
                //array('path' => 'modules/admin/models/', 'namespace' => 'AdminModel_'),
                //'plugin' => array('path' => '../library', 'namespace' => '',),
                'plugin' => array('path' => 'modules/default/plugins/', 'namespace' => 'Plugin_',),
        )));
        
        $autoLoader->registerNamespace('GB_');
        
        $this->config = new Zend_Config_Ini(CONFIG_PATH . '/application.ini', APPLICATION_ENV);
        $register = Zend_Registry::getInstance();
        $register->set('config', $this->config);
        
        Zend_Layout::startMvc(array('layoutPath' => APPLICATION_PATH . '/modules/default/layouts', 'layout' => 'layout'));
        
        $view = Zend_Layout::getMvcInstance()->getView();
        $view->doctype('XHTML1_TRANSITIONAL');
        
        $front = Zend_Controller_Front::getInstance();
        
        $moduleDir = $front->getModuleDirectory();
        
        $front->setControllerDirectory(array(
            'default' => '../application/modules/default/controllers',
            //'admin' => '../application/modules/admin/controllers'
        ));
        
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $viewRenderer->initView();
        $viewRenderer->view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'App_Controller_Plugin_Layout ');
        
        $this->_initClasses();
        
        // Return it so that it can be stored by the bootstrap
        return $autoLoader;
    }
    
    protected function _initDb() {
        
        $registry = Zend_Registry::getInstance();
        $config = $registry->get('config');
        $db = Zend_Db::factory($config->resources->db);
        
        Zend_Db_Table_Abstract::setDefaultAdapter($db);
    }
    
    protected function _initUrl() {
        //echo __FILE__.":18->_initUrl()<br /><br />";
        //TODO: jebuz -> implementar _initUrl()
    }
    
    protected function _initSiteRoutes() {
        
        $front = Zend_Controller_Front::getInstance();
        $router = $front->getRouter();
        
        /*if(($this->currentModule == 'admin') || ($this->admin)) {
            $router->addConfig(new Zend_Config_Ini(APPLICATION_PATH . '/configs/routesAdmin.ini'), 'routes');
        } else {*/
            $listRoutes = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini');
            
            foreach ($listRoutes->routes as $ruta=>$values) {
                $type = '';
                $route = '';
                $reverse = '';
                $defaults = array();
                $maps = array();
                
                foreach ($values as $k=>$v) {
                    if ($k == 'defaults') {
                        foreach ($v as $defaults=>$accion) {
                            $defaults[$defaults] = $accion;
                        }
                    } else if ($k == 'map') {
                        foreach ($v as $map=>$var) {
                            $maps[$map] = $var;
                        }
                    } else if ($k == 'type') {
                        $type = $v;
                    } else if ($k == 'route') {
                        $route = $v;
                    } else if ($k == 'reverse') {
                        $reverse = $v;
                    }
                }
                
                switch ($type) {
                    case 'Zend_Controller_Router_Route':   $newRoute = new Zend_Controller_Router_Route($route, $defaults);break;
                    case 'Zend_Controller_Router_Route_Regex':   $newRoute = new Zend_Controller_Router_Route_Regex($route, $defaults, $maps, $reverse);break;
                }

                $router->addRoute($ruta, $newRoute);
            }
        //}
    }
    
    protected function _initClasses() {
        require_once (MOD_PATH.'/class/LoadAdministracion.php');
        require_once (MOD_PATH.'/class/LoadCatalogacion.php');
        require_once (MOD_PATH.'/class/LoadCirculacion.php');
        require_once (MOD_PATH.'/class/LoadConectividad.php');
        require_once (MOD_PATH.'/class/LoadReportes.php');
        require_once (MOD_PATH.'/class/LoadRespaldos.php');
    }
    
    protected function _initAcl() {
        $acl = new Zend_Acl();
        
        $controllers = array (
            'administracion',
            'circulacion',
            'catalogacion',
            'reportes',
            'respaldos',
            'conectividad',
        );
        
        $acl->add(new Zend_Acl_Resource('error'));
        foreach ($controllers as $controller) {
            $acl->add(new Zend_Acl_Resource($controller));
        }
        
        $roles = Rol::GetRoles();
        foreach ($roles as $role) {
            $acl->addRole(new Zend_Acl_Role(strtolower($role->nombre)));
            
            $acl->allow(strtolower($role->nombre), "error");
            
            foreach ($controllers as $controller) {
                $privilegios = $role->GetPrivilegios();
                
                if (isset($privilegios[strtolower($role->nombre)]['allow.'.$controller])) {
                    $acl->allow(strtolower($role->nombre), $controller);
                } else {
                    $acl->deny(strtolower($role->nombre), $controller);
                }
            }
        }

        $registry = Zend_Registry::getInstance();
        $registry->set('acl', $acl);
    }
}